<template>
  <div>
    <router-link to="menu/add">
      <el-button>添加</el-button>
    </router-link>

    <el-table
      :data="tableData"
      style="width: 100%"
      row-key="id"
      lazy
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column label="标题" prop="title"></el-table-column>
      <el-table-column label="类型" prop="type">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type == 1">目录</el-tag>
          <el-tag v-else type="danger">菜单</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="图标" prop="icon">
        <template slot-scope="scope">
          <i :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column label="地址" prop="url"> </el-table-column>
      <el-table-column label="状态" prop="status" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 3" type="success">正常</el-tag>
          <el-tag v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            circle
            @click="edit(scope.row.id)"
            icon="el-icon-edit"
          ></el-button>

          <el-button
            type="danger"
            @click="handleDelete(scope.row.id)"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<style>
</style>

<script>
export default {
  data() {
    return {
      tableData: [],
    };
  },
  created() {
    this.http.get("/menulist?istree=1").then((res) => {
      this.tableData = res.data.list;
    });
  },
  methods: {
    // 编辑
    edit(id) {
      this.$router.push("/layout/menu/" + id);
    },
    // 删除，需要二次确认
    handleDelete(id) {
      this.$confirm("确认删除？")
        .then((_) => {
          //  确认删除
          console.log("确认删除");
          this.doDel(id);
        })
        .catch((_) => {
          //  关闭弹窗
          console.log("关闭弹窗");
        });
    },
    doDel(id) {
      this.http.post("/menudelete", { id }).then((res) => {
        console.log("删除已执行");
        // this.tableData = res.list;
      });
    },
  },
};
</script>